//
//  TaskDetailViewController.m
//  Gunz
//
//  Created by Joggy on 16/4/13.
//  Copyright © 2016年 NUPT. All rights reserved.
//

#import "TaskDetailViewController.h"
#import "GZConst.h"
#import "GZNetRequest.h"
#import "GZNetRequest.h"
#import "QYNotice.h"
#import "GZLoginState.h"

@interface TaskDetailViewController ()

@end

@implementation TaskDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    itemName = @[@"任务单号:", @"数据类型:", @"任务种类:", @"开始日期:", @"截止日期:", @"描述:", @"模板:"];
    itemData = @[@"25235", @"文本", @"群体任务", _data[@"starttime"], _data[@"overtime"], _data[@"describe"], _data[@"templete"]];
    itemView = [[NSMutableArray alloc] initWithCapacity:itemName.count];
    
    if(_isMineRelease) {
        itemBgColor = [UIColor whiteColor];
    }
    else {
        itemBgColor = [UIColor colorWithRed:0.898 green:0.898 blue:0.898 alpha:1];
    }
    
    [self prepareForNav];
    [self prepareForLayout];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    UIImage *image1 = [UIImage imageNamed:@"插图3"];
//    UIImage *image2 = [UIImage imageNamed:@"插图1"];
//    NSDictionary *para = @{@"isreal": UIImagePNGRepresentation(image1).description, @"photoback": UIImagePNGRepresentation(image2).description, @"idnum": @"412702"};
//    [[GZNetRequest getPOSTManager] POST:@"http://180.209.64.49:8002/user/verified" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@", responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@", error);
//    }];
    
}

- (void)prepareForNav {
    [super prepareForNav];
    //导航左按键
    UIImage *image = [UIImage imageNamed:@"all_back"];
    UIButton *leftBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftBarButton.frame = CGRectMake(0, 0, image.size.width, image.size.height);
    leftBarButton.layer.position = CGPointMake(22, 22);
    [leftBarButton setImage:[image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftBarButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftBarButton];
    UIImage *rightImage = [UIImage imageNamed:@"task_share"];
    UIButton *rightBarButton = [UIButton buttonWithType:UIButtonTypeSystem];
    rightBarButton.frame = CGRectMake(0, 0, rightImage.size.width, rightImage.size.height);
    rightBarButton.layer.position = CGPointMake([GZConst getScreenWidth] - 22, 22);
    [rightBarButton setImage:[rightImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarButton];
    self.title = @"详情";
}

- (void)prepareForLayout {
    [super prepareForLayout];
    scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, [GZConst getScreenWidth], [GZConst getScreenHeight] - 64 - 56)];
    scroll.backgroundColor = [UIColor clearColor];
    scroll.showsVerticalScrollIndicator = false;
    scroll.showsHorizontalScrollIndicator = false;
    scroll.contentSize = CGSizeMake(scroll.frame.size.width, scroll.frame.size.height + 1);
    [self.view addSubview:scroll];
    //任务图标
    UIImageView *icon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 66, 66)];
    icon.layer.position = CGPointMake(19 + 33, 22+33);
//    icon.image = [UIImage imageNamed:@"mine_icon_holder"];
    NSString *imgURL = @"http://180.209.64.49:8001/";
    imgURL = [imgURL stringByAppendingString:_data[@"icontask"]];
    [icon sd_setImageWithURL:[NSURL URLWithString:imgURL] placeholderImage:[UIImage imageNamed:@"mine_icon_holder"]];
    [scroll addSubview:icon];
    //任务名字
    UILabel *taskname = [[UILabel alloc] initWithFrame:CGRectMake(icon.frame.origin.x + icon.frame.size.width + 22, icon.frame.origin.y, [GZConst getScreenWidth]*0.5, 18)];
    taskname.numberOfLines = 2;
    taskname.textColor = TEXT_TITLE_COLOR;
//    taskname.text = @"简果公司数据采集";
    taskname.text = _data[@"name"];
    [taskname sizeToFit];
    [scroll addSubview:taskname];
    //价值
    UILabel *value = [[UILabel alloc] initWithFrame:CGRectMake([GZConst getScreenWidth] - 59, taskname.frame.origin.y, 49, 18)];
    value.textColor = [UIColor colorWithRed:0.945 green:0.596 blue:0.25 alpha:1];
    value.textAlignment = NSTextAlignmentRight;
//    value.text = @"5¥";
    NSString *priceStr = _data[@"faretask"];
    priceStr = [priceStr.description stringByAppendingString:@"¥"];
    value.text = priceStr;
    [scroll addSubview:value];
    //发布者
    UIButton *userName = [UIButton buttonWithType:UIButtonTypeSystem];
    userName.frame = CGRectMake(taskname.frame.origin.x, icon.frame.origin.y + icon.frame.size.height - 14, [GZConst getScreenWidth]*0.5, 14);
//    NSString *userNameStr = @"简果发布";
    NSString *userNameStr = _data[@"ownuser"];
    [userName setAttributedTitle:[[NSAttributedString alloc] initWithString:userNameStr attributes:@{NSForegroundColorAttributeName: TEXT_DETAIL_COLOR, NSFontAttributeName: [UIFont systemFontOfSize:10]}] forState:UIControlStateNormal];
    userName.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [userName addTarget:self action:@selector(userDetail) forControlEvents:UIControlEventTouchUpInside];
    [scroll addSubview:userName];
    //任务详情
    for(int i = 0; i < itemName.count; i++) {
        UIView *vie = [[UIView alloc] init];
        UILabel *arr = [[UILabel alloc] init];
        if(i == 0) {
            vie.frame = CGRectMake(95, 110, [GZConst getScreenWidth] - 95 - 18, 30);
        }
        else {
            UIView *fontVie = itemView[i - 1];
            vie.frame = CGRectMake(95, fontVie.frame.origin.y + fontVie.frame.size.height + 5, [GZConst getScreenWidth] - 95 - 18, 30);
        }
        arr.frame = CGRectMake(5, 0, vie.frame.size.width - 10, 18);
        arr.numberOfLines = 0;
        arr.textColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:1];
        arr.text = itemData[i];
        [arr sizeToFit];
        CGRect tem = vie.frame;
        tem.size.height = arr.frame.size.height + 12;
        vie.frame = tem;
        CGPoint arrTem = arr.layer.position;
        arrTem.y = vie.frame.size.height/2;
        arr.layer.position = arrTem;
        vie.backgroundColor = itemBgColor;
        vie.layer.cornerRadius = 4;
        [vie addSubview:arr];
        itemView[i] = vie;
        [scroll addSubview:vie];
    }
    //任务详情标题
    for(int i = 0; i < itemName.count; i++) {
        UIView *vieN = itemView[i];
        UILabel *labN = [[UILabel alloc] initWithFrame:CGRectMake(0, vieN.frame.origin.y, 90, 30)];
        labN.textColor = TEXT_DETAIL_COLOR;
        labN.textAlignment = NSTextAlignmentRight;
        labN.text = itemName[i];
        [scroll addSubview:labN];
    }
    UIView *lasVie = itemView.lastObject;
    CGSize scrSiz = scroll.contentSize;
    scrSiz.height = (lasVie.frame.origin.y + lasVie.frame.size.height) > ([GZConst getScreenHeight] - 64 - 56) ? (lasVie.frame.origin.y + lasVie.frame.size.height) : ([GZConst getScreenHeight] - 64 - 56 + 1);
    scroll.contentSize = scrSiz;
    if(!_isBarButtonHidden) {
        if(_isMineRelease) {
            _myBut = [[BarButMy alloc] init];
            [_myBut.giveUpBut addTarget:self action:@selector(giveUpItem) forControlEvents:UIControlEventTouchUpInside];
            [_myBut.alterBut addTarget:self action:@selector(alterItem) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:_myBut];
        }
        else {
            _barButton = [[BarButtonView alloc] initWithFrame:CGRectMake(0, [GZConst getScreenHeight] - 64 - 56, [GZConst getScreenWidth], BBViewHeight) name:@"我要完成该任务"];
            [_barButton.but addTarget:self action:@selector(signUp) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:_barButton];
        }
    }
}

- (void)userDetail {
    
}

- (void)alterItem {
    
}

- (void)giveUpItem {
    NSLog(@"%@", @"give up");
}

#pragma mark --报名任务
- (void)signUp {
    AFHTTPSessionManager *manager = [GZNetRequest getPOSTManager];
    NSDictionary *para = @{@"tid": @"1", @"uid": [GZLoginState getCurrentLoginUserID], @"sid": @"1"};
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:@"http://180.209.64.49:8001/task/taskJoin" parameters:para progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"%@", @"success");
        NSLog(@"%@", responseObject);
        NSDictionary *dic = responseObject;
        if([dic[@"status"] isEqualToString:@"success"]) {
            [QYNotice showSuccess:@"报名成功" toView:self.view];
            [QYNotice dismissWithDelay:3];
        }
        else {
            [QYNotice showError:@"报名失败" toView:self.view];
            [QYNotice dismissWithDelay:3];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", @"failure");
        NSLog(@"%@", error);
        [QYNotice showError:@"报名失败" toView:self.view];
        [QYNotice dismissWithDelay:3];
    }];
}

- (void)back {
    [self.navigationController popViewControllerAnimated:true];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
